package com.da.lon.wang.xlg.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.Classify1Adapter;
import com.da.lon.wang.xlg.bean.MSearchType;
import com.da.lon.wang.xlg.bean.ResultSearchBean;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultCategory;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultOther.SearchResultOtherItems;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Classify1Activity extends BaseActivity implements OnClickListener {

    private Context context;
    private String searchContent = null;
    private String cid = null;
    private String sattr = null;
    private String otherName;
    private int requestCode;
    private int clickPosition;

    private HttpLoader<ResultSearchBean> loader;
    private List<MSearchType> list = new ArrayList<MSearchType>();
    private List<SearchResultCategory> list2 = new ArrayList<SearchResultCategory>();
    private List<SearchResultOtherItems> list3 = new ArrayList<SearchResultOtherItems>();

    private Classify1Adapter adapter;

    private ImageButton leftImageButton;
    private TextView rightTextView;
    private TextView nameTextView;
    private TextView itemTextView;
    private RelativeLayout classifyLayout;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ouka_classify1);

        context = this;

        Intent intent = getIntent();
        searchContent = intent.getStringExtra("searchContent");
        cid = intent.getStringExtra("cid");
        sattr = intent.getStringExtra("sattr");

        initView();
        initData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        String classy_name = PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.CLASSY_NAME, "");

        if (classy_name.equals("")) {
            itemTextView.setText("全部");
        } else {
            itemTextView.setText(classy_name);
        }

    }

    private void initData() {
        // TODO Auto-generated method stub

        listView = (ListView) findViewById(R.id.ouka_classify1_lv);
        adapter = new Classify1Adapter(list, context, otherName, clickPosition);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub

                requestCode = 0;
                clickPosition = position;
                list3.clear();
                if (list != null && list.size() > position && list.get(position).getOther() != null)
                    list3.addAll(list.get(position).getOther().getItems());

                Intent intent = new Intent(Classify1Activity.this, Classify2Activity.class);
                intent.putExtra("otherlist", (Serializable) list3);
                intent.putExtra("style", "2");
                intent.putExtra("position", position);
                startActivityForResult(intent, requestCode);

            }
        });

        loader = new HttpLoader<ResultSearchBean>(context, XLGConstants.URL + "Search-index", ResultSearchBean.class,
                60 * 1000);
        AjaxParams params = new AjaxParams();
        params.put("token", XLGDataUtils.encrypt2MD5("Search" + "2015#$%" + "index"));
        params.put("name", searchContent);
        params.put("cid", cid);
        params.put("sattr", sattr);

        loader.setManagerListener(new OnWebLoadListener<ResultSearchBean>() {

            @Override
            public void OnStart() {
                // TODO Auto-generated method stub
                showDialog();
            }

            @Override
            public void OnError(String error) {
                // TODO Auto-generated method stub
                dismissDialog();
            }

            @Override
            public void onSuccess(ResultSearchBean t) {
                // TODO Auto-generated method stub
                dismissDialog();

                if (t != null && "000000".equals(t.getCode())) {


                    if (t.getResult().getOther() == null) {

                        list.clear();

                        adapter.notifyDataSetChanged();
                    } else {
                        list.clear();
                        for (int i = 0; t.getResult().getOther() != null && i < t.getResult().getOther().size(); i++) {
                            MSearchType type = new MSearchType();
                            type.setItem(null);
                            type.setOther(t.getResult().getOther().get(i));
                            list.add(type);
                        }
                    }

                    list2.clear();
                    list2.addAll(t.getResult().getCategory());

                    adapter.notifyDataSetChanged();

                    PreferenceUtils.getInstance().SetSettingString(PreferencesConfig.SEARCH_SATTR, "");

                } else {
                    if (t != null) {
                        Toast.makeText(Classify1Activity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        loader.loadData(params);


    }

    private void initView() {
        // TODO Auto-generated method stub

        leftImageButton = (ImageButton) findViewById(R.id.ouka_classify1_left);
        rightTextView = (TextView) findViewById(R.id.ouka_classify1_right);
        nameTextView = (TextView) findViewById(R.id.ouka_classify1_name);
        itemTextView = (TextView) findViewById(R.id.ouka_classify1_item);
        classifyLayout = (RelativeLayout) findViewById(R.id.ouka_classify1_rlayout);

        leftImageButton.setOnClickListener(this);
        rightTextView.setOnClickListener(this);
        classifyLayout.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
            case R.id.ouka_classify1_left:
                finish();
                break;

            case R.id.ouka_classify1_right:
                Intent intent = new Intent(Classify1Activity.this, ResultSearchActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("cid", cid);
                bundle.putString("sattr", sattr);
                bundle.putString("searchContent", searchContent);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();

                break;

            case R.id.ouka_classify1_rlayout:

                Intent intent1 = new Intent(Classify1Activity.this, Classify2Activity.class);
                intent1.putExtra("categorylist", (Serializable) list2);
                intent1.putExtra("style", "1");

                startActivity(intent1);

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        if (data != null) {

            switch (requestCode) {
                case 0:

                    int pos = data.getIntExtra("position", 0);
                    SearchResultOtherItems item = (SearchResultOtherItems) data.getSerializableExtra("otherName");
                    if (list != null) {
                        list.get(pos).setItem(item);
                    }
                    adapter.notifyDataSetChanged();


                    break;

                default:
                    break;
            }
        }

    }
}
